#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5+5;
int n, m;
ll A[N], c[N], x[114];
ll a[N];
char C[N];
vector<int> R[N], RR[N];
ll ans[N];
int main(){
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    cin >> n >> m;
    for(int i = 1; i <= n; i++){
        scanf("%lld", &A[i]);
    }
    for(int i = 1; i <= m; i++){
        scanf("%lld", &c[i]);
    }
    int K = (n > 1 ? log2(n-1)+1 : 1);
    for(int i = K; i >= 1; i--){
        getchar();
        scanf("%s", C+1);
        //cout << C+1 << endl;
        for(int j = 1; j <= pow(2, i); j++){
            R[i].push_back(C[j]-'0');
        }
    }
    int T;
    cin >> T;
    while(T--){
        for(int i = 0; i < 4; i++){
            scanf("%lld", &x[i]);
        }
        ll sum = 0;
        for(int i = 1; i <= n; i++){
            a[i] = A[i] ^ x[i%4];
            sum += a[i];
        }
        sum *= (m/2);
        srand(time(0));
        printf("%lld\n", (rand()*rand())%sum+1);
    }
    return 0;
}
